package com.zzp.zjs_bos.web.action.base;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zzp.zjs_bos.dao.IDecidedDao;
import com.zzp.zjs_bos.service.IDecidedService;
import com.zzp.zjs_bos.service.IFunctionService;
import com.zzp.zjs_bos.service.INoticebillService;
import com.zzp.zjs_bos.service.IRegionService;
import com.zzp.zjs_bos.service.IRoleService;
import com.zzp.zjs_bos.service.IStaffService;
import com.zzp.zjs_bos.service.ISubareaService;
import com.zzp.zjs_bos.service.IUserService;
import com.zzp.zjs_bos.service.IWorkbillService;
import com.zzp.zjs_bos.service.IWorkmanageService;
import com.zzp.zjs_bos.utils.PageBean;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T>{
	
	protected T model; 		
	public T getModel() {
		return model;
	}
	
	@Resource
	protected IUserService userService;
	
	@Resource
	protected IStaffService staffService;
		
	@Resource
	protected IRegionService regionService;
	
	@Resource
	protected 	ISubareaService subareaService;
	
	@Resource
	protected IDecidedService decidedService;
	
	@Resource
	protected INoticebillService noticebillService;
	
	@Resource
	protected IWorkbillService workbillService;
	
	@Resource
	protected IWorkmanageService workmanageService;
	
	@Resource
	protected IFunctionService functionService;
	
	@Resource
	protected IRoleService roleService;
	
	
	protected PageBean pageBean = new PageBean();
	
	public void setPage(int page) {
		pageBean.setCurrPage(page);
	}
	public void setRows(int rows) {
		pageBean.setPageSize(rows);
	}
	protected DetachedCriteria detachedCriteria;
	
	
	public BaseAction(){
		ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
		Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
		Class<T> doMain =  (Class<T>) actualTypeArguments[0];
		//将离线查询的语句放到构造方法里，是能获得泛型里的对象
		pageBean.setDetachedCriteria(DetachedCriteria.forClass(doMain));
		try {
			model = doMain.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	public void writePageBean2Json(PageBean pageBean,final String[] excludes) {
		PropertyFilter filter = new PropertyFilter() {
			
			public boolean apply(Object obj, String name, Object value) {
				for (String string : excludes) {
					if(name.equalsIgnoreCase(string)){
						//false代表过滤
						return false;
					}
				}
				return true;
			}
		};
		Map map = new HashMap();
		map.put("total",pageBean.getTotalCount());
		map.put("rows",pageBean.getRows());
		
		String json = JSON.toJSONString(map,filter,SerializerFeature.DisableCircularReferenceDetect);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		try {
			ServletActionContext.getResponse().getWriter().write(json);
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	public void writeList2Json(List list,final String[] excludes){
		PropertyFilter filter = new PropertyFilter() {
			
			public boolean apply(Object obj, String name, Object value) {
				for (String string : excludes) {
					if(name.equalsIgnoreCase(string)){
						
						return false;
					}
				}
				return true;
			}
		};
		
		String json = JSON.toJSONString(list,filter,SerializerFeature.DisableCircularReferenceDetect);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		try {
			ServletActionContext.getResponse().getWriter().write(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void writeObject2Json(Object obj,final String[] excludes){
		
		PropertyFilter filter = new PropertyFilter() {
			
			public boolean apply(Object obj, String name, Object value) {
				for (String string : excludes) {
					if(string.equals(name)){
						return false;
					}
				}
				
				return true;
			}
		};
		
		 String json = JSON.toJSONString(obj,filter,SerializerFeature.DisableCircularReferenceDetect);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		try {
			ServletActionContext.getResponse().getWriter().write(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
}
